package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.dto.SysDeptDto;
import com.cmti.skeleton.entity.SysDept;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SysDeptMapper {

    @Delete({
        "delete from sys_dept",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Delete({
        "delete from sys_dept",
        "where P_ID = #{pid,jdbcType=BIGINT}"
    })
    int deleteByParentId(Long pid);

    @Insert({
        "insert into sys_dept (NAME, P_ID, ",
        "REMARK, STATUS)",
        "values (#{name,jdbcType=VARCHAR}, #{pId,jdbcType=BIGINT}, ",
        "#{remark,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(SysDept record);

    @InsertProvider(type = SysDeptSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(SysDept record);

    @Select({
        "select",
        "ID, NAME, P_ID, REMARK, STATUS",
        "from sys_dept",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column = "ID", property = "id", jdbcType = JdbcType.BIGINT, id = true)
        ,
        @Result(column = "NAME", property = "name", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "P_ID", property = "pId", jdbcType = JdbcType.BIGINT)
        ,
        @Result(column = "REMARK", property = "remark", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "STATUS", property = "status", jdbcType = JdbcType.INTEGER)
    })
    SysDept selectByPrimaryKey(Long id);

    /**
     * 获取带上级部门名称的部门列表
     *
     * @return
     */
    @Select({
        "select",
        "d.ID, d.NAME, d.P_ID, d.REMARK, d.STATUS, p.NAME as parentDeptName",
        "from sys_dept d",
        "left join sys_dept p on d.P_ID=p.ID"
    })
    List<SysDeptDto> selectWithParentDeptName();

    /**
     * 根据父部门获取部门列表
     *
     * @param pid
     * @return
     */
    @Select({
        "select",
        "ID, NAME, P_ID, REMARK, STATUS",
        "from sys_dept",
        "WHERE STATUS=0 AND P_ID=#{pid}"
    })
    List<SysDeptDto> selectByParentId(long pid);

    @Select({
        "select count(0)",
        "from sys_dept",
        "WHERE P_ID=#{pid}"
    })
    int countByParentId(long pid);

    @UpdateProvider(type = SysDeptSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SysDept record);

    @Update({
        "update sys_dept",
        "set NAME = #{name,jdbcType=VARCHAR},",
        "P_ID = #{pId,jdbcType=BIGINT},",
        "REMARK = #{remark,jdbcType=VARCHAR},",
        "STATUS = #{status,jdbcType=INTEGER}",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(SysDept record);
    
    @Select({"SELECT * FROM sys_dept where ID = #{deptId}"})
    SysDept selectById(long deptId);
}
